package com.approval.approvalapp.index.dbsx.tshf.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.NewTshfDb;
import com.approval.approvalapp.bean.TshfdbBean;
import com.approval.approvalapp.index.dbsx.tshf.TsHfActivity;
import com.approval.approvalapp.index.dbsx.tshf.adapter.TshfdbAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseFragment;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.Bind;


/**
 * A simple {@link Fragment} subclass.
 */
public class TsHfDbFragment extends BaseFragment {

    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_ll)
    LinearLayout empty_ll;
    private int pagenum = 1;
    private NewTshfDb tshfdbBeans;
    private TshfdbAdapter tshfdbAdapter;
    private List<NewTshfDb.PendingListBean> pendingList;
    public static  TsHfDbFragment instance;



    @Override
    protected int getLayoutId() {

        instance = this;
        return R.layout.fragment_ts_hf_db;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        instance = null;
    }

    @Override
    public void initView(View view) {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        tshfdbAdapter = new TshfdbAdapter();
        xrv.setAdapter(tshfdbAdapter);
        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                addData(pagenum);
            }
        });
    }

    @Override
    public void initData() {

    }

    public void refreshData() {
        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", "1");
        jsonObject.put("username", SpUtil.getString(this.getActivity(),SpUtil.USER_NAME,""));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_ts_db_list", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                Log.d("TsHfDbFragmentsss", str);

                tshfdbBeans = JSON.parseObject(str, NewTshfDb.class);
                pendingList = tshfdbBeans.getPendingList();
                if (pendingList == null || pendingList.size() == 0){
                    empty_ll.setVisibility(View.VISIBLE);
                    return;
                }
                empty_ll.setVisibility(View.GONE);
                tshfdbAdapter.setTshfdbBeanList(tshfdbBeans.getPendingList());
                TsHfActivity tsHfActivity = (TsHfActivity) TsHfDbFragment.this.getActivity();
                tsHfActivity.refreshTab0(tshfdbBeans.getPendingCount() + "");
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    private void addData(int pagenum) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", pagenum + "");
        jsonObject.put("username", SpUtil.getString(this.getActivity(),SpUtil.USER_NAME,""));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_ts_db_list", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                NewTshfDb mptblDbBeans = JSON.parseObject(str, NewTshfDb.class);
                List<NewTshfDb.PendingListBean> sspendingList = mptblDbBeans.getPendingList();
                if (sspendingList == null || sspendingList .size() == 0){
                    xrv.setNoMore(true);
                }else {
                    for (int i = 0; i < sspendingList.size(); i++) {
                        TsHfDbFragment.this.pendingList.add(sspendingList.get(i));
                    }
                    tshfdbAdapter.setTshfdbBeanList(TsHfDbFragment.this.pendingList);
                    xrv.loadMoreComplete();
                }

            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
                xrv.loadMoreComplete();
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
                xrv.loadMoreComplete();
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}
